package com.zhiyou.app.shop.website.service.impl;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.IPwdUpdateLogDao;
import com.zhiyou.app.shop.website.dao.IUserInfoDao;
import com.zhiyou.app.shop.website.dao.pojo.ActivityApply;
import com.zhiyou.app.shop.website.dao.pojo.PwdUpdateLog;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.service.IUserInfoService;

@Service(value="userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {
	
	@Autowired
	private IUserInfoDao userInfoDao;
	@Autowired
	private IPwdUpdateLogDao pwdUpdateLogDao;

	public void findUser(Page page, User user) {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		//查询条件  会员ID 会员类型  级别  推荐人  安置人  
		if(user !=null){
			if(user.getUserAccount() != null && !"".equals(user.getUserAccount())){
				criteria.add(Expression.like("userAccount", user.getUserAccount()));
			}
			if(user.getActivity() != null){
				if(ShopConstants.TRUE.equals(user.getActivity())){  //0  激活
					criteria.add(Expression.eq("activity", user.getActivity()));
				}else if(ShopConstants.FALSE.equals(user.getActivity())){
					criteria.add(Expression.isNull("activity"));
				}
			}
			if(user.getEnterType() != null && "2".equals(user.getEnterType())){
				criteria.add(Expression.eq("enterType", user.getEnterType()));
				//criteria.add(Expression.isEmpty("mainAccount"));
				criteria.add(Restrictions.or(Restrictions.eq("mainAccount", ""), Property.forName("mainAccount").isNull())); 
				//criteria.add(Property.forName("mainAccount").isNull());
			}
			/*if(user.getLevel() != null && !"".equals(user.getLevel())){
				criteria.add(Expression.eq("level", user.getLevel()));
			}
			if(user.getRecommendId() != null && !"".equals(user.getRecommendId())){
				criteria.add(Expression.eq("recommendId", user.getRecommendId()));
			}
			if(user.getParentId() != null && !"".equals(user.getParentId())){
				criteria.add(Expression.eq("parentId", user.getParentId()));
			}*/
			if(user.getRegisterDate() != null && !"".equals(user.getRegisterDate())){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//
				String date = df.format(user.getRegisterDate());
				try {
					criteria.add(Expression.between("registerDate", Timestamp.valueOf(date+" 00:00:00.0"), Timestamp.valueOf(date+" 23:59:59.0")));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		userInfoDao.findByCriteria(page, criteria, Order.desc("registerDate"));
	}
	
	public static void main(String[] args) {
		DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date myDate1 = dateFormat1.parse("2014-04-01 00:00:00.0");
			System.out.println(new Timestamp(myDate1.getTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public User findByAccount(String account) {
		List<User> list = userInfoDao.findByProperty("userAccount", account);
		if(list != null && list.size() >0 ){
			return list.get(0);
		}
		return null;
	}

	public void save(User user) {
		userInfoDao.save(user);
		
	}

	public void savePwd(User user, PwdUpdateLog pwd1, PwdUpdateLog pwd2,
			PwdUpdateLog pwd3) {
		if(pwd1 != null){
			pwdUpdateLogDao.save(pwd1);
		}
		if(pwd2 != null){
			pwdUpdateLogDao.save(pwd2);
		}
		if(pwd3 != null){
			pwdUpdateLogDao.save(pwd3);
		}
		userInfoDao.save(user);
		
	}

	public List<User> findAll() {
		return userInfoDao.findAll();
	}

	public void markUser(String[] userIds, String type) {
		User user = null;
		if("1".equals(type)){  //标记公司账户   
			for (String userId : userIds) {
				user = userInfoDao.findById(userId);
				user.setRole(ShopConstants.MEMBER_COMPANY);
				userInfoDao.update(user);
			}
		}else if("2".equals(type)){ //取消标记公司
			for (String userId : userIds) {
				user = userInfoDao.findById(userId);
				user.setRole(ShopConstants.MEMBER);
				userInfoDao.update(user);
			}
		}
		
	}
}
